package com.achievement.service.impl;

import com.achievement.domain.bo.HonorApplyInfoBO;
import com.achievement.domain.bo.ReviewPlanBO;
import com.achievement.domain.dto.honor.ReviewPlanDTO;
import com.achievement.domain.entity.ReviewPlanEntity;
import com.achievement.domain.param.honor.HonorApplyInfoParam;
import com.achievement.domain.param.honor.ReviewPlanParam;
import com.achievement.domain.vo.honor.ReviewPlanBookExpertNumVO;
import com.achievement.domain.vo.honor.ReviewPlanVO;
import com.achievement.mapper.ReviewPlanMapper;
import com.achievement.mappings.ReviewPlanMapping;
import com.achievement.service.IAchievementFileService;
import com.achievement.service.IReviewPlanService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ReviewPlanServiceImpl extends ServiceImpl<ReviewPlanMapper, ReviewPlanEntity> implements IReviewPlanService {
        @Autowired
        private ReviewPlanMapping mapping;
        @Autowired
        private ReviewPlanMapper mapper;
        @Autowired
        private IAchievementFileService fileService;

        @Override
        public List<ReviewPlanBookExpertNumVO> reviewPlanBookExpertNumByPage(ReviewPlanParam param) {
            ReviewPlanBO bo= mapping.param2Bo(param);
            if(StringUtils.isNotEmpty(param.getReviewSchemeId())&&!param.getReviewSchemeId().equals("[]")){
                List<String> reviewSchemeIds=Arrays.stream(param.getReviewSchemeId().split(",")).filter(id -> !id.trim().isEmpty()).collect(Collectors.toList());
                bo.setReviewSchemeIdList(reviewSchemeIds);
            }
            List<ReviewPlanBookExpertNumVO> voList=mapper.reviewPlanBookExpertNumByPage(bo);
            if(!ObjectUtils.isNotEmpty(voList)){return voList;}
            List<String> ids=voList.stream()
                    .map(ReviewPlanBookExpertNumVO::getId)  //
                    .collect(Collectors.toList()); //reviewPlanBookExpertNumByReviewPlanIds
            bo.setIds(ids);
            List<ReviewPlanBookExpertNumVO> voListByIds=mapper.reviewPlanBookExpertNumByReviewPlanIds(bo);
            List<ReviewPlanBookExpertNumVO> returnList=new ArrayList<>();
            if(ObjectUtils.isNotEmpty(voListByIds)){
                for (int i=0;i<voListByIds.size();i++){
                    String id=voListByIds.get(i).getId();
                    if(returnList.stream().filter(t->t.getId().equals(id)).count()==0){
                        ReviewPlanBookExpertNumVO vo=voListByIds.get(i);
                        if(StringUtils.isNotEmpty(voListByIds.get(i).getReviewObjectId())){
                            List<ReviewPlanBookExpertNumVO> selectList=voListByIds.stream().filter(t->t.getId().equals(id)).collect(Collectors.toList());
                            Integer applyBookNum=0;
                            List<String> reviewObjIds=new ArrayList<>();
                            for (ReviewPlanBookExpertNumVO mo:selectList) {
                                if(!reviewObjIds.contains(mo.getReviewObjectId())){reviewObjIds.add(mo.getReviewObjectId());applyBookNum++;}
                            }
                            vo.setReviewHonorApplyBookNum(applyBookNum.toString());
                            Integer reviewExpertNum=0;
                            List<String> reviewerIds=new ArrayList<>();
                            for (ReviewPlanBookExpertNumVO mo:selectList) {
                                if(mo.getReviewerId()!=null&&!reviewerIds.contains(mo.getReviewerId())){reviewerIds.add(mo.getReviewerId());reviewExpertNum++;}
                            }
                            vo.setReviewExpertNum(reviewExpertNum.toString());
                        }else{
                            vo.setReviewHonorApplyBookNum("0");
                            vo.setReviewExpertNum("0");
                        }
                        returnList.add(vo);
                    }
                }
            }
            return returnList;
        }
    @Override
    public List<HashMap<String, Integer>> listStatistics(ReviewPlanParam param){
        ReviewPlanBO bo= mapping.param2Bo(param);
        if(StringUtils.isNotEmpty(param.getReviewSchemeId())){
            List<String> reviewSchemeIds=Arrays.stream(param.getReviewSchemeId().split(",")).filter(id -> !id.trim().isEmpty()).collect(Collectors.toList());
            bo.setReviewSchemeIdList(reviewSchemeIds);
        }
        return mapper.listStatistics(bo);
    }
        @Override
        public List<ReviewPlanEntity> getListByConditions(ReviewPlanDTO patentDTO) {
            QueryWrapper<ReviewPlanEntity> queryWrapper = new QueryWrapper();
            List<ReviewPlanEntity> list = this.list(queryWrapper);
            return list;
        }
    @Override
    public List<String> getApplyInfoIds() {
        QueryWrapper<ReviewPlanEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("business_type","honorApply");
        List<ReviewPlanEntity> list = this.list(queryWrapper);
        return list.stream().map(ReviewPlanEntity::getApplyInfoId).filter(str -> str != null && !str.isEmpty()).collect(Collectors.toList());
    }



        @Override
    public ReviewPlanVO getInfoById(String id) {
        ReviewPlanEntity entity = this.getById(id);
        ReviewPlanVO ReviewPlanVO = mapping.entity2Vo(entity);
        String fileIds = entity.getFileId();
        if (com.common.utils.StringUtils.hasText(fileIds)) {
            String[] fileIdArr = fileIds.split(",");
            List<Map<String, String>> fileInfo = fileService.getFileInfoByFileIds(fileIdArr);
            ReviewPlanVO.setFileList(fileInfo);
        }
        return ReviewPlanVO;
    }
        @Override
        public boolean add(ReviewPlanDTO dto) {
            ReviewPlanEntity entity = mapping.dto2Entity(dto);
            entity.setBusinessType("honorApply");
            entity.setReviewPlanStatus("notStart");
            return this.save(entity);
        }


        @Override
        public boolean update(ReviewPlanDTO dto) {
            ReviewPlanEntity entity = mapping.dto2Entity(dto);
            return this.updateById(entity);
        }

        @Override
        public boolean updateStatusById(ReviewPlanDTO dto){
            LambdaUpdateWrapper<ReviewPlanEntity> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(ReviewPlanEntity::getReviewPlanStatus, dto.getReviewPlanStatus())
                    .eq(ReviewPlanEntity::getId, dto.getId());
            return this.update(null, wrapper);
        }

        @Override
        public boolean batchRemove(List<String> ids) {
            boolean returnResult=false;
            returnResult=this.removeByIds(ids);
            return returnResult;
        }


}
